<?php

namespace app\api\controller;

use app\data\model\DataQiyeXinxiku;
use app\data\model\DataZhengceku;
use app\data\model\DataZhengcekuJinzhan;
use app\data\model\PolicyBasics;
use app\data\model\PolicyMatterTypes;
use app\data\service\UserAdminService;
use think\admin\Controller;
use think\facade\Db;

class Index extends Controller
{

    public function indexLbu()
    {
        return_msg(200, 'success', sysdata('indexlunbo'));
    }



    //返回PC首页统计数据
    public function getPcIndexData(){

        $res['zc_count'] = Db::name("data_zhengceku")->where('deleted',0)->count();
        $res['msjx_count'] = Db::name("data_msjx")->where('deleted',0)->count();

        $res['qhwy_all_count'] = Db::name("data_qhwy_user")->where('deleted',0)->count();

        $res['qhwy_ycl_count'] = Db::name("data_qhwy_user")->where('tj_status','<>',1)->count();

        return_msg(200, '查询成功', $res);
    }


    //账号授权
    public function zhanghaoCreate()
    {

        $openid = input('openid');
        $phone  = input('phone');

        if (empty($openid)) {
            return_error('获取失败：{openid}');
        }
        if (empty($phone)) {
            return_error('获取失败：{phone}');
        }
        // 判断数据库有没有这个人
        $re = Db::name('data_user')->where(['openid1' => $openid, 'deleted' => 0])->find();


        //人员存在
        if (!empty($re)) {

            if ($re['sf'] == 1) {


                $xinxiku = Db::name("data_qiye_xinxiku")->where('phone', $re['phone'])->where('deleted', 0)->find();

                if (empty($xinxiku)) {

                    //为了兼容他人身份，查询是否认证
                    $is_re_rz_count = Db::name("data_qiye_rzinfo")->where('user_id', $re['id'])->where('deleted', 0)->count();
                    if ($is_re_rz_count > 0) {
                        $re['is_rz'] = 1;
                    } else {
                        $re['is_rz'] = 0;
                    }

                } else {//信息库匹配成功

                    //获取最新的认证记录
                    $info = Db::name('data_qiye_rzinfo')->where('user_id', $re['id'])->where('deleted', 0)->find();

                    if (empty($info)) {
                        $data_qiye_rzinfo = [
                            'user_id'     => $re['id'],
                            'qymc'        => $xinxiku['qymc'],
                            'tyxydm'      => $xinxiku['tyxydm'],
                            'yyzz'        => $xinxiku['yyzz'],
                            'sbr_type'    => 1,
                            'sbr_xm'      => $xinxiku['sbr_xm'],
                            'sbr_phone'   => $xinxiku['phone'],
                            'xzbm'        => $xinxiku['xzbm'],
                            'qybq'        => $xinxiku['qybq'],
                            'create_time' => time(),
                            'qrxxk_id'    => $xinxiku['id'],
                            'qiyesf'      => 1,
                        ];

                        Db::name("data_qiye_rzinfo")->insert($data_qiye_rzinfo);

                        Db::name("data_user")->where('id', $re['id'])->update(['nickname' => $xinxiku['qymc']]);
                        $re['nickname'] = $xinxiku['qymc'];


                    } else {

                        if ($info['qrxxk_id'] != $xinxiku['id']) {//生成最新的认证

                            Db::name("data_qiye_rzinfo")->where('id', $info['id'])->where('deleted', 0)->update(['deleted' => 1]);

                            $data_qiye_rzinfo = [
                                'user_id'     => $re['id'],
                                'qymc'        => $xinxiku['qymc'],
                                'tyxydm'      => $xinxiku['tyxydm'],
                                'yyzz'        => $xinxiku['yyzz'],
                                'sbr_type'    => 1,
                                'sbr_xm'      => $xinxiku['sbr_xm'],
                                'sbr_phone'   => $xinxiku['phone'],
                                'xzbm'        => $xinxiku['xzbm'],
                                'qybq'        => $xinxiku['qybq'],
                                'create_time' => time(),
                                'qrxxk_id'    => $xinxiku['id'],
                                'qiyesf'      => 1,
                            ];

                            Db::name("data_qiye_rzinfo")->insert($data_qiye_rzinfo);

                            Db::name("data_user")->where('id', $re['id'])->update(['nickname' => $xinxiku['qymc']]);
                            $re['nickname'] = $xinxiku['qymc'];
                        }
                    }

                    $re['is_rz'] = 1;
                }
            }

            return_msg(200, 'success', $re);
        } else {

            $fwgphone = Db::name("data_fwg_phone")->where('phone', $phone)->find();
            // 插入数据库
            $array             = [];
            $array['openid1']  = $openid;
            $array['phone']    = $phone;
            $array['password'] = md5(123456);
            $array['status']   = 1;
            $array['headimg']  = sysconf('site_url') . '/upload/tx.jpg';
            $array['id']       = Db::name("data_user")->insertGetId($array);


            if (empty($fwgphone)) {

                $array['sf']       = 1;
                $array['nickname'] = '用户' . $this->generateRandomUsername();


                $xinxiku = Db::name("data_qiye_xinxiku")->where('phone', $phone)->where('deleted', 0)->find();

                if (empty($xinxiku)) {

                    $array['is_rz'] = 0;

                } else {//信息库匹配成功
                    $data_qiye_rzinfo = [
                        'user_id'     => $array['id'],
                        'qymc'        => $xinxiku['qymc'],
                        'tyxydm'      => $xinxiku['tyxydm'],
                        'yyzz'        => $xinxiku['yyzz'],
                        'sbr_type'    => $xinxiku['sbr_type'],
                        'sbr_xm'      => $xinxiku['sbr_xm'],
                        'sbr_phone'   => $xinxiku['phone'],
                        'xzbm'        => $xinxiku['xzbm'],
                        'qybq'        => $xinxiku['qybq'],
                        'create_time' => time(),
                        'qrxxk_id'    => $xinxiku['id'],
                        'qiyesf'      => 1,
                    ];

                    Db::name("data_qiye_rzinfo")->insert($data_qiye_rzinfo);

                    $array['nickname'] = $xinxiku['qymc'];
                    $array['is_rz']    = 1;
                }

            } else {
                //服务员
                $array['sf']       = 2;
                $array['nickname'] = $fwgphone['nickname'];
            }

            Db::name("data_user")->where('id', $array['id'])->update(['sf' => $array['sf'], 'nickname' => $array['nickname']]);
            return_msg(200, 'success', $array);
        }
    }


    //查看用户信息
    public function getUserinfo()
    {

        $user_id = input('user_id');

        if (empty($user_id)) {
            return_error('缺少参数');
        }

        $info = Db::name("data_user")->where('id', $user_id)->find();

        return_msg(200, '查询成功', $info);
    }


    function generateRandomUsername($length = 6)
    {
        $characters      = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $username        = '';
        $characterLength = strlen($characters);

        for ($i = 0; $i < $length; $i++) {
            $username .= $characters[rand(0, $characterLength - 1)];
        }

        return $username;
    }

    //获取政策库分类
    public function getZckfl()
    {

        $list = Db::name('data_zhengceku_fl')->where('deleted', 0)->select()->toArray();

        return_msg(200, 'success!', $list);
    }


    //获取政策库分类
    public function policyTypeList()
    {
        $list           = Db::name('data_zhengceku_fl')->field(['id', 'name'])->where('deleted', 0)->select()->toArray();
        $policyTypeList = PolicyBasics::convertToIdValueArray();
        return_msg(200, 'success!', [
            'type'         => $list,
            'policy_level' => $policyTypeList,
        ]);
    }


    //首页全部政策列表
    public function getIndexAllZckList(){

        $list = Db::name("data_zhengceku")->where('deleted', 0)->order('ctrq desc')->limit(10)->select()->toArray();

        foreach ($list as &$item){

            $item['ctrq_txt']   = $item['ctrq'] ? date('Y-m-d', $item['ctrq']) : '';

        }
        return_msg(200, 'success!', $list);
    }

    //首页政策列表
    public function getIndexZckList()
    {

        $fl_id = input('fl_id');
        $user_id = input('user_id');
        // if (empty($fl_id)) {
        //     return_error('缺少参数');
        // }


        if(empty($user_id)){
            return_error('缺少参数');
        }

        $list = Db::name("data_zhengceku")->where('deleted', 0);



        $rz_info = Db::name("data_qiye_rzinfo")->where('user_id', $user_id)->where('deleted', 0)->find();


        if(!empty($rz_info)){
            $qybqs = explode(',', $rz_info['qybq']);

            $list = $list->where(function ($query) use ($qybqs) {
                foreach ($qybqs as $qybq) {
                    $query->whereFindInSet('qybq', $qybq, 'OR');
                }
            });

            $list = $list->order('ctrq desc')->limit(10)->select()->toArray();

            foreach ($list as &$item) {
                if (!empty($item['admin_id'])) {
                    $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
                } else {
                    $item['admin_name'] = '';
                }

                $item['sbksrq_txt'] = $item['sbksrq'] ? date('Y-m-d', $item['sbksrq']) : '';
                $item['ctrq_txt']   = $item['ctrq'] ? date('Y-m-d', $item['ctrq']) : '';
                if ($item['riqitype'] == 1) {
                    if (!empty($item['sbjzrq'])) {
                        if ($item['sbjzrq'] > time()) {
                            // 计算剩余秒数
                            $remainingSeconds = $item['sbjzrq'] - time();
                            if ($remainingSeconds >= 86400) { // 大于等于 1 天
                                // 计算剩余天数
                                $remainingDays = floor($remainingSeconds / 86400);
                                $item['syts']  = "{$remainingDays}天";
                            } else {
                                // 小于 1 天，计算剩余小时数
                                $remainingHours = floor($remainingSeconds / 3600);
                                $item['syts']   = "{$remainingHours}小时";
                            }
                        } else {
                            $item['syts'] = '已结束';
                        }
                    } else {
                        $item['syts'] = '';
                    }
                } else {
                    $item['syts'] = '长期';
                }
            }

        }else{

            $list = [];
        }





        // $this->success('success', $list, 200);
        return_msg(200, 'success!', $list);
    }

    //政策匹配列表
    public function policyList()
    {

        $user_id = input('user_id');
        $fl_id       = input('fl_id');
        $levelId     = input('level_id');
        $isFinancial = input('is_financial');
        $search      = input('search');
        $time_type      = input('time_type');
        $list        = DataZhengceku::query()->where('deleted', 0);
        if ($levelId !== '' && $levelId !== null) {
            $list = $list->where('level_id', $levelId);
        }
        if ($fl_id !== '' && $fl_id !== null) {
            $list = $list->where('wzfl', $fl_id);
        }
        if ($search !== '' && $search !== null) {
            $list = $list->where('title', 'like', "%{$search}%");
        }
        if ($isFinancial !== '' && $isFinancial !== null) {
            $list = $list->where('is_financial', $isFinancial);
        }

        if (!empty($user_id)) {

            $rz_info = Db::name("data_qiye_rzinfo")->where('user_id', $user_id)->where('deleted', 0)->find();
            if(!empty($rz_info)){
                $qybqs = explode(',', $rz_info['qybq']);

                $list = $list->where(function ($query) use ($qybqs) {
                    foreach ($qybqs as $qybq) {
                        $query->whereFindInSet('qybq', $qybq, 'OR');
                    }
                });


                if(empty($time_type)){
                    $list = $list->order('ctrq desc');
                }else{
                    if($time_type==1){
                        $list = $list->order('ctrq asc');
                    }else{
                        $list = $list->order('ctrq desc');
                    }
                }
                $list = $list->select()->toArray();
                // $list     = Db::name("data_zhengceku")->where('wzfl', $fl_id)->where('deleted', 0)->order('create_at desc')->limit(20)->select()->toArray();
                foreach ($list as &$item) {
                    if (!empty($item['admin_id'])) {
                        $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
                    } else {
                        $item['admin_name'] = '-';
                    }
                    $item['sbksrq_txt'] = $item['sbksrq'] ? date('Y-m-d', $item['sbksrq']) : '';
                    $item['ctrq_txt']   = $item['ctrq'] ? date('Y-m-d', $item['ctrq']) : '';
                    if ($item['riqitype'] == 1) {
                        if (!empty($item['sbjzrq'])) {
                            if ($item['sbjzrq'] > time()) {
                                // 计算剩余秒数
                                $remainingSeconds = $item['sbjzrq'] - time();
                                if ($remainingSeconds >= 86400) { // 大于等于 1 天
                                    // 计算剩余天数
                                    $remainingDays = floor($remainingSeconds / 86400);
                                    $item['syts']  = $remainingDays . '天';
                                } else {
                                    // 小于 1 天，计算剩余小时数
                                    $remainingHours = floor($remainingSeconds / 3600);
                                    $item['syts']   = $remainingHours . '小时';
                                }
                            } else {
                                $item['syts'] = '已结束';
                            }
                        } else {
                            $item['syts'] = '-';
                        }
                    } else {
                        $item['syts'] = '长期';
                    }
                    $item['level_name'] = PolicyBasics::RELEASE_LEVEL_MAP[$item['level_id']] ?? '-';
                }

            }else{

                $list = [];
            }

        }else{

            if(empty($time_type)){
                $list = $list->order('ctrq desc');
            }else{
                if($time_type==1){
                    $list = $list->order('ctrq asc');
                }else{
                    $list = $list->order('ctrq desc');
                }
            }
            $list = $list->select()->toArray();
            // $list     = Db::name("data_zhengceku")->where('wzfl', $fl_id)->where('deleted', 0)->order('create_at desc')->limit(20)->select()->toArray();
            foreach ($list as &$item) {
                if (!empty($item['admin_id'])) {
                    $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
                } else {
                    $item['admin_name'] = '-';
                }
                $item['sbksrq_txt'] = $item['sbksrq'] ? date('Y-m-d', $item['sbksrq']) : '';
                $item['ctrq_txt'] = $item['ctrq'] ? date('Y-m-d', $item['ctrq']) : '';
                if ($item['riqitype'] == 1) {
                    if (!empty($item['sbjzrq'])) {
                        if ($item['sbjzrq'] > time()) {
                            // 计算剩余秒数
                            $remainingSeconds = $item['sbjzrq'] - time();
                            if ($remainingSeconds >= 86400) { // 大于等于 1 天
                                // 计算剩余天数
                                $remainingDays = floor($remainingSeconds / 86400);
                                $item['syts'] = $remainingDays . '天';
                            } else {
                                // 小于 1 天，计算剩余小时数
                                $remainingHours = floor($remainingSeconds / 3600);
                                $item['syts'] = $remainingHours . '小时';
                            }
                        } else {
                            $item['syts'] = '已结束';
                        }
                    } else {
                        $item['syts'] = '-';
                    }
                } else {
                    $item['syts'] = '长期';
                }
                $item['level_name'] = PolicyBasics::RELEASE_LEVEL_MAP[$item['level_id']] ?? '-';
            }
        }



        // $this->success('success', $list, 200);
        return_msg(200, 'success!', $list);
    }

    //获取政策库文章详情
    public function getZckInfo()
    {
        $id = input('id');
        if (empty($id)) {
            return_error('缺少id');
        }
        $info = Db::name("data_zhengceku")->where('id', $id)->find();
        if (empty($info)) {
            return_error('未找到文章记录');
        }
        if (!empty($info['qybq'])) {
            $qybq_array       = explode(',', $info['qybq']);
            $info['qybq_txt'] = '';
            $i                = 0;
            foreach ($qybq_array as $qybq) {
                if ($i > 0) {
                    $info['qybq_txt'] .= ',';
                }
                $info['qybq_txt'] .= Db::name("data_qiye_biaoqian")->where('id', $qybq)->value('name');
                $i++;
            }
        } else {
            $info['qybq_txt'] = '';
        }

        if (!empty($info['admin_id'])) {
            $info['admin_name'] = Db::name("system_user")->where('id', $info['admin_id'])->value('nickname');
        } else {
            $info['admin_name'] = '';
        }

        if (!empty($info['ctrq'])) {

            $info['ctrq_txt'] = date('Y-m-d', $info['ctrq']);

        } else {
            $info['ctrq_txt'] = '';
        }

        if (!empty($info['sbksrq'])) {

            $info['sbksrq_txt'] = date('Y-m-d', $info['sbksrq']);

        } else {
            $info['sbksrq_txt'] = '';
        }

        if ($info['riqitype'] == 1) {
            if (!empty($info['sbjzrq'])) {

                $info['sbjzrq_txt'] = date('Y-m-d', $info['sbjzrq']);

            } else {
                $info['sbjzrq_txt'] = '';
            }
        } else {

            $info['sbjzrq_txt'] = '长期';
        }

        $info['matter_type_txt'] = [];
        if (!empty($info['matter_type_id']) && $info['matter_type_id'] != '0') {
            $ids                     = explode(',', $info['matter_type_id']);
            $info['matter_type_txt'] = PolicyMatterTypes::query()->whereIn('id', $ids)
                ->where('status', '=', 1)
                ->where('deleted', '=', 0)
                ->column('matter_name');
        }
        $info['progress'] = DataZhengcekuJinzhan::query()->where('zc_id', '=', $id)->select();


        $info['level_name'] = PolicyBasics::RELEASE_LEVEL_MAP[$info['level_id']] ?? '-';
        return_msg(200, 'success', $info);

    }


    //获取政策文件地址
    public function getWenjianUrl()
    {

        $id = input('id');
        if (empty($id)) {
            return '';
        }

        $url = Db::name("data_zhengceku")->where('id', $id)->value('wenjian_url');

        return_msg(200, 'success', ['url' => $url]);
    }

    //获取政策最新进展
    public function getZcNewjinzhan()
    {

        $id = input('id');
        if (empty($id)) {
            return '';
        }

        $list = Db::name("data_zhengceku_jinzhan")->where('zc_id', $id)->select()->toArray();

        return_msg(200, 'success', $list);
    }

    //政策库 展示更多
    public function getZckAllList()
    {
        $page  = input('page', 1);
        $limit = input('limit', 10);
        $key   = input('key');
        $fl_id = input('fl_id');

        if (empty($fl_id)) {
            return_error('缺少参数');
        }

        $where['wzfl']    = $fl_id;
        $where['deleted'] = 0;


        if (!empty($key)) {
            $where[] = ['title', 'like', '%' . $key . '%'];
        }

        $list = Db::name('data_zhengceku')->where($where)->order('create_at desc')->paginate($limit, false, ['page' => $page]);

        // 引用方式遍历集合并修改数据
        $list->each(function (&$item)
        {
            if (!empty($item['admin_id'])) {
                $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
            } else {
                $item['admin_name'] = '';
            }

            if (!empty($item['sbjzrq'])) {
                if ($item['sbjzrq'] > time()) {

                    // 计算剩余秒数
                    $remainingSeconds = $item['sbjzrq'] - time();

                    if ($remainingSeconds >= 86400) { // 大于等于 1 天
                        // 计算剩余天数
                        $remainingDays = floor($remainingSeconds / 86400);
                        $item['syts']  = "{$remainingDays}天";
                    } else {
                        // 小于 1 天，计算剩余小时数
                        $remainingHours = floor($remainingSeconds / 3600);
                        $item['syts']   = "{$remainingHours}小时";
                    }
                } else {
                    $item['syts'] = '已结束';
                }

            } else {
                $item['syts'] = '';
            }

            return $item;
        });

        // 获取分页数据
        $data = [
            'list'      => $list->items(),
            'total'     => $list->total(),
            'last_page' => $list->lastPage(),
        ];

        // 输出分页数据
        return_msg(200, '查询成功', $data);
    }


    //专属政策
    public function getZszcAllList()
    {
        $page    = input('page', 1);
        $limit   = input('limit', 10);
        $key     = input('key');
        $fl_id   = input('fl_id');
        $user_id = input('user_id');

        if (empty($fl_id)) {
            return_error('缺少参数fl_id');
        }

        if (empty($user_id)) {
            return_error('缺少参数user_id');
        }

        $qiye_info = Db::name("data_qiye_rzinfo")->where('user_id', $user_id)->where('deleted', 0)->find();
        if (empty($qiye_info)) {
            return_error('未查到企业信息');
        }
        //企业信息库id为$qiye_info['qrxxk_id']
        $where['wzfl']    = $fl_id;
        $where['deleted'] = 0;


        if (!empty($key)) {
            $where[] = ['title', 'like', '%' . $key . '%'];
        }

        $whereRaw = '';
        // 构造查询条件来匹配 qybq 字段
        if (!empty($qiye_info['qybq'])) {
            // 创建 FIND_IN_SET 的 SQL 查询条件
            $qiye_qybq_list = explode(',', $qiye_info['qybq']);

            //            $where[] = ['FIND_IN_SET(qybq, '.$qiye_info['qybq'].')' ];
            //            $where[] = ["FIND_IN_SET(qybq,'1,2')" ];

            // 使用 FIND_IN_SET 来检查 qybq 字段中是否包含指定的值
            $i = 0;
            foreach ($qiye_qybq_list as $value) {
                //                $qybq_conditions[] = ['FIND_IN_SET(?, qybq)', $value];
                if ($i > 0) {
                    $whereRaw .= ' or ';
                }
                $whereRaw .= 'FIND_IN_SET(' . $value . ', qybq)';
                $i++;
            }
        }

        //注册资金
        //        $where[] = ['qs_zczj','<=',$qiye_info['zczj']];
        //        $where[] = ['js_zczj','>=',$qiye_info['zczj']];


        $list = Db::name('data_zhengceku')->where($where)->whereRaw($whereRaw)->order('create_at desc')->paginate($limit, false, ['page' => $page]);

        // 引用方式遍历集合并修改数据
        $list->each(function (&$item)
        {
            if (!empty($item['admin_id'])) {
                $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
            } else {
                $item['admin_name'] = '';
            }

            if (!empty($item['sbjzrq'])) {
                if ($item['sbjzrq'] > time()) {

                    // 计算剩余秒数
                    $remainingSeconds = $item['sbjzrq'] - time();

                    if ($remainingSeconds >= 86400) { // 大于等于 1 天
                        // 计算剩余天数
                        $remainingDays = floor($remainingSeconds / 86400);
                        $item['syts']  = "{$remainingDays}天";
                    } else {
                        // 小于 1 天，计算剩余小时数
                        $remainingHours = floor($remainingSeconds / 3600);
                        $item['syts']   = "{$remainingHours}小时";
                    }
                } else {
                    $item['syts'] = '已结束';
                }

            } else {
                $item['syts'] = '';
            }

            return $item;
        });

        // 获取分页数据
        $data = [
            'list'      => $list->items(),
            'total'     => $list->total(),
            'last_page' => $list->lastPage(),
        ];

        // 输出分页数据
        return_msg(200, '查询成功', $data);
    }






    //获取奖补公示列表
    public function jiangbugongshiList()
    {

        $list = Db::name("data_jiangbugongshi")->where('deleted', 0)->order('sort desc')->select()->toArray();

        foreach ($list as &$item) {

        }

        // 输出分页数据
        return_msg(200, '查询成功', $list);
    }


    //查看奖补公示详情
    public function getJiangbugongshiInfo()
    {

        $id = input('id');

        if (empty($id)) {
            return_error('缺少id');
        }

        $info = Db::name("data_jiangbugongshi")->where('id', $id)->find();

        $info['list'] = Db::name("data_jiangbugongshi_mark")->where('deleted', 0)->where('jbgs_id',$id)->order('sbsj desc')->select()->toArray();


        return_msg(200, '查询成功', $info);

    }



    //获取所有企业标签
    public function getAllQybqList()
    {

        $list = Db::name("data_qiye_biaoqian")->where('is_kj', 1)->select()->toArray();

        return_msg(200, '', $list);
    }


    //政策匹配
    public function getZcppList()
    {
        $page    = input('page', 1);
        $limit   = input('limit', 10);
        $fl_id   = input('fl_id');
        $qybq_id = input('qybq_id');

        //        if(empty($qybq_id)){
        //            return_error('缺少参数标签id');
        //        }

        if (empty($fl_id)) {
            return_error('缺少参数分类id');
        }

        $where['deleted'] = 0;
        $where['wzfl']    = $fl_id;

        //        if(!empty($key)){
        //            $where[] = ['title','like','%'.$key.'%'];
        //        }

        if (empty($qybq_id)) {
            $whereRaw = "id > 0";
        } else {
            $whereRaw = "FIND_IN_SET('" . $qybq_id . "',qybq)";
        }


        //注册资金
        //        $where[] = ['qs_zczj','<=',$qiye_info['zczj']];
        //        $where[] = ['js_zczj','>=',$qiye_info['zczj']];


        $list = Db::name('data_zhengceku')->where($where)->whereRaw($whereRaw)->order('create_at desc')->paginate($limit, false, ['page' => $page]);

        // 引用方式遍历集合并修改数据
        $list->each(function (&$item)
        {
            if (!empty($item['admin_id'])) {
                $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
            } else {
                $item['admin_name'] = '';
            }

            if (!empty($item['sbksrq'])) {

                $item['sbksrq_txt'] = date('Y-m-d', $item['sbksrq']);

            } else {
                $item['sbksrq_txt'] = '';
            }

            if ($item['riqitype'] == 1) {

                if (!empty($item['sbjzrq'])) {
                    if ($item['sbjzrq'] > time()) {

                        // 计算剩余秒数
                        $remainingSeconds = $item['sbjzrq'] - time();

                        if ($remainingSeconds >= 86400) { // 大于等于 1 天
                            // 计算剩余天数
                            $remainingDays = floor($remainingSeconds / 86400);
                            $item['syts']  = "{$remainingDays}天";
                        } else {
                            // 小于 1 天，计算剩余小时数
                            $remainingHours = floor($remainingSeconds / 3600);
                            $item['syts']   = "{$remainingHours}小时";
                        }
                    } else {
                        $item['syts'] = '已结束';
                    }

                } else {
                    $item['syts'] = '';
                }

            } else {

                $item['syts'] = '长期';
            }

            return $item;
        });

        // 获取分页数据
        $data = [
            'list'      => $list->items(),
            'total'     => $list->total(),
            'last_page' => $list->lastPage(),
        ];

        // 输出分页数据
        return_msg(200, '查询成功', $data);
    }


    //申报通告
    public function getSbtgList()
    {
        $page  = input('page', 1);
        $limit = input('limit', 10);
        $key   = input('key');
        //        $fl_id = input('fl_id');
        $user_id = input('user_id');

        if (empty($user_id)) {
            return_error('缺少参数user_id');
        }

        $qiye_info = Db::name("data_qiye_rzinfo")->where('user_id', $user_id)->where('deleted', 0)->find();
        if (empty($qiye_info)) {
            return_error('未查到企业信息');
        }

        //        $where['wzfl'] = $fl_id;
        $where['deleted'] = 0;
        $where[]          = ['sbksrq', '<', time()];
        $where[]          = ['sbksrq', '>', 0];


        if (!empty($key)) {
            $where[] = ['title', 'like', '%' . $key . '%'];
        }

        $whereRaw = '';
        // 构造查询条件来匹配 qybq 字段
        if (!empty($qiye_info['qybq'])) {
            // 创建 FIND_IN_SET 的 SQL 查询条件
            $qiye_qybq_list = explode(',', $qiye_info['qybq']);

            //            $where[] = ['FIND_IN_SET(qybq, '.$qiye_info['qybq'].')' ];
            //            $where[] = ["FIND_IN_SET(qybq,'1,2')" ];

            // 使用 FIND_IN_SET 来检查 qybq 字段中是否包含指定的值
            $i = 0;
            foreach ($qiye_qybq_list as $value) {
                //                $qybq_conditions[] = ['FIND_IN_SET(?, qybq)', $value];
                if ($i > 0) {
                    $whereRaw .= ' or ';
                }
                $whereRaw .= 'FIND_IN_SET(' . $value . ', qybq)';
                $i++;
            }
        }

        //注册资金
        //        $where[] = ['qs_zczj','<=',$qiye_info['zczj']];
        //        $where[] = ['js_zczj','>=',$qiye_info['zczj']];


        $list = Db::name('data_zhengceku')->where($where)->whereRaw($whereRaw)->order('create_at desc')->paginate($limit, false, ['page' => $page]);

        // 引用方式遍历集合并修改数据
        $list->each(function (&$item)
        {
            if (!empty($item['admin_id'])) {
                $item['admin_name'] = Db::name("system_user")->where('id', $item['admin_id'])->value('nickname');
            } else {
                $item['admin_name'] = '';
            }

            if (!empty($item['sbksrq'])) {

                $item['sbksrq_txt'] = date('Y-m-d', $item['sbksrq']);

            } else {
                $item['sbksrq_txt'] = '';
            }

            if (!empty($item['sbjzrq'])) {
                if ($item['sbjzrq'] > time()) {

                    // 计算剩余秒数
                    $remainingSeconds = $item['sbjzrq'] - time();

                    if ($remainingSeconds >= 86400) { // 大于等于 1 天
                        // 计算剩余天数
                        $remainingDays = floor($remainingSeconds / 86400);
                        $item['syts']  = "{$remainingDays}天";
                    } else {
                        // 小于 1 天，计算剩余小时数
                        $remainingHours = floor($remainingSeconds / 3600);
                        $item['syts']   = "{$remainingHours}小时";
                    }
                } else {
                    $item['syts'] = '已结束';
                }

            } else {
                $item['syts'] = '';
            }

            return $item;
        });

        // 获取分页数据
        $data = [
            'list'      => $list->items(),
            'total'     => $list->total(),
            'last_page' => $list->lastPage(),
        ];

        // 输出分页数据
        return_msg(200, '查询成功', $data);
    }

    public function checkEnterpriseUser()
    {
        $enterpriseList = DataQiyeXinxiku::query()->where('status', '=', 1)
            ->where('deleted', '=', 0)->select();
        foreach ($enterpriseList as $enterprise) {
            UserAdminService::handleEnterpriseUser($enterprise->toArray());
        }
        return_msg(200, '检测成功');
    }
}
